<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // let obj2 = {
        //     a:obj1.a,
        //     b:{...obj1.b}
        // }
        // let obj2 = JSON.parse(JSON.stringify(obj1))
        // obj2.a = 5
        // obj2.b.c = 6
        // console.log('obj1', obj1);
        // console.log('obj2', obj2);
        let obj1 = {
            a: 1,
            b: {
                c: 2,
                d: {
                    e: 3,
                    f: [4, 5, {
                        g: 6,
                        h: 7,
                        i: [8, 9, 
                            {j: 10}
                        ]
                    }]
                }
            }
        }

        function copy(obj) {
            if (obj instanceof Object) {
                let newObj = {}
                for (let key in obj) {
                    if (typeof obj[key] === 'object') {
                        newObj[key] = copy(obj[key]) //copy(obj1.b) 
                    } else {
                        newObj[key] = obj[key]
                    }
                }
                return newObj
            } else if (obj instanceof Array) {
                let newArr = []
                for (let i = 0; i < obj.length; i++) {
                    if (typeof obj[i] === 'object') {
                        newArr.push(copy(obj[i]))
                    } else {
                        newArr.push(obj[i])
                    }
                }
                return newArr
            }
        }

        let newObj = copy(obj1)
        newObj.b.d.f[2].i[2].j = 88
        console.log(newObj);
        console.log(obj1);
    </script>
</body>

</html>